import { JsonAsset, TextAsset, __private, resources } from 'cc';
import { ConfigBase } from './ConfigBase';
import { ResMgr } from '../../../ISUtility/Script/ResMgr';
import { LevelConfig } from './LevelConfig';

export class GameConfig {

    private static _inst: GameConfig;
    public static get inst(): GameConfig {
        if (this._inst == null) {
            this._inst = new GameConfig();
        }
        return this._inst;
    }

    private _dic: { [key: string]: ConfigBase } = {};

    public Get<T extends ConfigBase>(type: __private._types_globals__Constructor<T>, complete: (config: T) => void) {

        if (null != this._dic[type.name]) {
            complete(this._dic[type.name] as T);
        } else {
            ResMgr.inst.load("Bundle/GameConfig/" + type.name, JsonAsset, null, (err, json) => {
                if (!err) {
                    const obj = json.json!;
                    this._dic[type.name] = new(type);
                    (this._dic[type.name] as any).Datas = obj.Datas;
                    console.log("LevelConfig Load complete");
                    complete(this._dic[type.name] as T);
                } else { 
                    complete(null);
                }
            });
        }
    }
}



